import Vue from 'vue'
import Toast from './toast.vue'
import pageScroll from '../../../_common/js/utils/pageScroll'
Toast.new = (options = {}) => {
  const instance = new Vue({
    data: options,
    render (h) {
      return h(Toast, {
        props: options
      })
    }
  })
  const component = instance.$mount()
  document.body.appendChild(component.$el)
  pageScroll.lock(component.$el)

  const toast = instance.$children[0]
  const opr = {
    close () {
      const el = toast.$el
      pageScroll.unlock(el)
      el.parentNode && el.parentNode.removeChild(el)
      typeof toast.callback === 'function' && toast.callback()
    }
  }

  const timer = setTimeout(() => {
    clearTimeout(timer)
    opr.close()
  }, toast.timeout + 100)

  return opr
}

export default Toast
